//
//  SettingsViewController.m
//  ExhibitForcePrototype
//
//  Created by Georgi Bachvarov on 12/5/12.
//  Copyright (c) 2012 Dilyana Todorova. All rights reserved.
//

#import "SettingsViewController.h"
#import "SessionManager.h"
#import "MMAppDelegate.h"
#import <QuartzCore/QuartzCore.h>

#define WELCOME_TEXT_TEMPLATE @"Welcome %@!"

@interface SettingsViewController () 

@property (nonatomic, strong) NSString *username;

@end

@implementation SettingsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"123-id-card"] tag:1];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.passwordTextField.secureTextEntry = YES;
    [self updateInterfaceLayoutForLoggedIn:NO];
    [[SessionManager sharedInstance] setDelegate:self];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:223.f/256.f green:223.f/256.f blue:223.f/256.f alpha:1.0f].CGColor, (id)[UIColor whiteColor].CGColor, nil];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.borderColor =[UIColor blackColor].CGColor;
    gradientLayer.borderWidth = 2.0f;
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [[[self.view.layer sublayers] objectAtIndex:0] setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
}

- (void) updateInterfaceLayoutForLoggedIn: (BOOL) isLoggedIn{
    self.usernameTextField.text = @"";
    self.passwordTextField.text = @"";
    self.usernameDisplayLabel.hidden = !isLoggedIn;
    self.accessoryUsernameDisplayLabel.hidden = !isLoggedIn;
    self.usernameTextField.hidden = isLoggedIn;
    self.passwordTextField.hidden = isLoggedIn;
    self.welcomeLabel.hidden = !isLoggedIn;
    self.welcomeLabel.text = [NSString stringWithFormat:WELCOME_TEXT_TEMPLATE, self.username];
    [self.loginButton setTitle: isLoggedIn? @"Logout" : @"Login" forState:UIControlStateNormal];
    self.usernameDisplayLabel.text = isLoggedIn ? self.username : @"";
    self.registerButton.hidden = isLoggedIn;
    MMAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithObjects:appDelegate.contentViewController, appDelegate.settingsViewController, nil];
    if (isLoggedIn){
        [viewControllers addObject:appDelegate.analyticsViewController];
    }
    appDelegate.mainTabBarViewController.viewControllers = viewControllers;
}

- (void)sessionManagerDidValidateUsername:(NSString *)username{
    self.username = username;
    [self.activityIndicator stopAnimating];
    [self updateInterfaceLayoutForLoggedIn:YES];
}

- (IBAction)loginButtonPressed:(id)sender {
    [self hideKeyboard];
    if (![[SessionManager sharedInstance] isValidSession]){
        [[SessionManager sharedInstance] validateSessionWithUsername:self.usernameTextField.text andPassword:self.passwordTextField.text];
        [[self activityIndicator] startAnimating];
    }else{
        [[SessionManager sharedInstance] invalidateSession];
        [self updateInterfaceLayoutForLoggedIn:NO];
    }
}

- (IBAction)registerButtonPressed:(id)sender {

}

- (void) hideKeyboard{
    [self.passwordTextField resignFirstResponder];
    [self.usernameTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
